﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Animation;

namespace MediaBrowser.CustomControls.Transitionals
{
    /// <summary>
    /// Logique d'interaction pour TranslateTransitionElement.xaml
    /// </summary>
    public partial class TranslateTransitionElement : UserControl
    {
        private ContentPresenter newContentPresenter;
        private bool isTransitioning;

        public TranslateTransitionElement()
        {
            InitializeComponent();
            newContentPresenter = new ContentPresenter();
        }

        public object ContentTransition
        {
            get { return (object)GetValue(ContentTransitionProperty); }
            set { SetValue(ContentTransitionProperty, value); }
        }
        public static readonly DependencyProperty ContentTransitionProperty =
            DependencyProperty.Register("ContentTransition",
                typeof(object),
                typeof(TranslateTransitionElement),
                new UIPropertyMetadata(null, OnContentTransitionChanged));

        public bool IsGoForward
        {
            get { return (bool)GetValue(IsGoForwardProperty); }
            set { SetValue(IsGoForwardProperty, value); }
        }

        public static readonly DependencyProperty IsGoForwardProperty =
            DependencyProperty.Register("IsGoForward", typeof(bool), typeof(TranslateTransitionElement));


        private static void OnContentTransitionChanged(object element, DependencyPropertyChangedEventArgs e)
        {
            TranslateTransitionElement te = element as TranslateTransitionElement;
            if (te != null)
            {
                te.BeginTransition();
            }
        }

        private void BeginTransition()
        {
            if (ContentTransition != null)
            {
                if (CtCenter.Content != null)
                {
                    if (!isTransitioning)
                    {
                        double direction = 1;
                        if (IsGoForward) { direction = 1; } else { direction = -1; }

                        TranslateTransform tCenter = new TranslateTransform(0, 0);
                        CtCenter.RenderTransform = tCenter;
                        CtCenter.RenderTransformOrigin = new Point(0.5, 0.5);

                        TranslateTransform tOld = new TranslateTransform(direction * this.ActualWidth, 0);
                        newContentPresenter.RenderTransform = tOld;
                        newContentPresenter.RenderTransformOrigin = new Point(0.5, 0.5);
                        newContentPresenter.Content = ContentTransition;
                        GrdMain.Children.Add(newContentPresenter);

                        DoubleAnimation oldContentAnimation = new DoubleAnimation(-direction * this.ActualWidth, new Duration(TimeSpan.FromMilliseconds(1000)));
                        oldContentAnimation.AccelerationRatio = 0.5;
                        oldContentAnimation.DecelerationRatio = 0.5;
                        tCenter.BeginAnimation(TranslateTransform.XProperty, oldContentAnimation);

                        DoubleAnimation newContentAnimation = new DoubleAnimation(0, new Duration(TimeSpan.FromMilliseconds(1000)));
                        newContentAnimation.AccelerationRatio = 0.5;
                        newContentAnimation.DecelerationRatio = 0.5;
                        newContentAnimation.Completed += newContentAnimation_Completed;
                        tOld.BeginAnimation(TranslateTransform.XProperty, newContentAnimation);
                        isTransitioning = true;
                    }
                }
                else
                {
                    CtCenter.Content = ContentTransition;
                    TranslateTransform tCenter = new TranslateTransform(0, 0);
                    CtCenter.RenderTransform = tCenter;
                    CtCenter.RenderTransformOrigin = new Point(0.5, 0.5);
                }
            }
        }

        void newContentAnimation_Completed(object sender, EventArgs e)
        {
            CtCenter.ClearValue(ContentPresenter.RenderTransformProperty);
            newContentPresenter.ClearValue(ContentPresenter.RenderTransformProperty);
            CtCenter.Content = ContentTransition;
            CtCenter.RenderTransform = new TranslateTransform(0, 0);
            GrdMain.Children.Remove(newContentPresenter);
            isTransitioning = false;
        }

    }
}
